//
//  GuahaoRecordViewController.m
//  安好
//
//  Created by lianchuang on 14-12-11.
//  Copyright (c) 2014年 lianchuangbrother. All rights reserved.
//

#import "GuahaoRecordViewController.h"
#import "MyGuahaoRecordViewController.h"
#import "GuahaoListCell.h"
#import "PictureViewController.h"

#define RGB(r, g, b)    [UIColor colorWithRed:(r)/255.f green:(g)/255.f blue:(b)/255.f alpha:1.f]

#define iPhone6Plus ([UIScreen instancesRespondToSelector:@selector(currentMode)] ? CGSizeEqualToSize(CGSizeMake(1242, 2208), [[UIScreen mainScreen] currentMode].size) : NO)


@interface GuahaoRecordViewController ()<UITableViewDataSource,UITableViewDelegate,MyGuahaoRecordViewControllerDelegate,PictureViewControllerDelegate>

/**
 *  挂号页面的tableView
 */
@property (strong, nonatomic) UITableView *tableView;

@property(nonatomic,strong)NSMutableArray * dataArray;
@property(nonatomic,strong)NSMutableArray * picSizeArray;

/**
 *  下拉加载页数
 */
@property (assign, nonatomic) NSInteger loadPage;

@property (nonatomic, strong) PictureViewController *picViewController;
@end

@implementation GuahaoRecordViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self makeNavBar];
    [self makeTableView];
    [self loadingRecordList];
}
#pragma mark - 导航条设置
-(void)makeNavBar
{
    self.navigationItem.title = @"挂号";
    //左按钮--返回
    UIBarButtonItem *backBtnImg = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"公用_返回.png"] style:UIBarButtonItemStylePlain target:self action:@selector(backClicked)];
    [backBtnImg setTintColor:[UIColor whiteColor]];
    self.navigationItem.leftBarButtonItem = backBtnImg;
    //右按钮-- “上传挂号记录”
    UIButton * recordBtnImg = [[UIButton alloc] initWithFrame:CGRectMake(10, 0, 90, 30)];
    [recordBtnImg setTitle:@"上传挂号单" forState:UIControlStateNormal];
    [recordBtnImg setTitleEdgeInsets:UIEdgeInsetsMake(0, 15, 0, 0)];
    recordBtnImg.titleLabel.font = [UIFont systemFontOfSize:15];
    [recordBtnImg addTarget:self action:@selector(uplodingGuahaoRecord) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:recordBtnImg];
}
/**
 * 返回
 */
-(void)backClicked
{
    [self.navigationController popViewControllerAnimated:YES];
}
/**
 *  触发进入 挂号记录--上传挂号单
 */
-(void)uplodingGuahaoRecord
{
    MyGuahaoRecordViewController * guahao = [[MyGuahaoRecordViewController alloc] init];
    guahao.delegate = self;
    [self.navigationController pushViewController:guahao animated:YES];
    
}

//挂号单上传成功返回来刷新页面
-(void)loadingRecordList
{
    self.dataArray = [NSMutableArray arrayWithCapacity:0];
    self.loadPage = 1;
    [self AFNetRequestGuahaoList];

}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (iPhone6Plus) {
        return 150;
    }else{
        return 125;
    }
}
-(void)AFNetRequestGuahaoList
{
    [self showActionView];
    NSString *postURL = [NSString stringWithFormat:@"%@user/uploadguahaolist",kBaseURL];
    //post参数列表
    NSDictionary *parameters = @{@"platform": kPlatform,@"version":kVersion,@"page":[NSString stringWithFormat:@"%d",self.loadPage]};
    //网络请求管理者
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    [manager POST:postURL parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
        
        [self hiddenActionView];
        
        NSNumber * statusNum = [responseObject objectForKey:@"status"];
        NSNumberFormatter* numberFormatter = [[NSNumberFormatter alloc] init];
        NSString * statusString = [numberFormatter stringFromNumber:statusNum];
        if ([statusString isEqualToString:@"0"]) {
            [self shouLabel:@"网络失败"];
            [self.tableView footerEndRefreshing];
        }else
        {
            self.tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
            if (self.loadPage == 1) {
                [self.dataArray removeAllObjects];
            }
            NSArray * listArray = responseObject[@"data"];
            if (self.loadPage == 1 && listArray.count == 0) {
                [self shouLabel:@"暂无挂号单记录"];
            }
            if (listArray != nil) {
                self.loadPage++;
                for (NSDictionary * dic in listArray) {
                    [self.dataArray addObject:dic];
                    
                }
                [self.tableView reloadData];
            }
            else
            {
                if (self.loadPage > 1) {
                    MyNSLog("下拉无数据");
                }
            }
            [self.tableView footerEndRefreshing];
            
        }
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
        [self hiddenActionView];
    }];



}
#pragma mark - 创建tableView 设置代理添加头视图
-(void)makeTableView
{
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight-iOS7TopMargin)style:UITableViewStyleGrouped];
    [self.view addSubview:self.tableView];
    self.tableView.backgroundColor = [ClassMethodsViewController colorWithHexString:BACK_BACKGROUNDCOLOR_16];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    //隐藏横向和竖向的滑条
    self.tableView.showsHorizontalScrollIndicator = NO;
    self.tableView.showsVerticalScrollIndicator = NO;
    self.tableView.separatorStyle =UITableViewCellSeparatorStyleNone;
    [self.tableView setSeparatorColor:RGB(221, 221, 221)];
    [self.tableView addFooterWithTarget:self action:@selector(AFNetRequestGuahaoList)];
    
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataArray.count;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if (iPhone6Plus) {
        return 12;
    }else{
        return 10;
    }
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0.01;
}
-(UITableViewCell * )tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    GuahaoListCell * cell = [tableView dequeueReusableCellWithIdentifier:@"ID"];
    if (cell == nil) {
        if (iPhone6Plus) {
            cell = [[[NSBundle mainBundle] loadNibNamed:@"GuahaoListCell" owner:self options:nil] lastObject];
        }else{
            cell = [[[NSBundle mainBundle] loadNibNamed:@"GuahaoListCell" owner:self options:nil] firstObject];
        }
    }
    NSDictionary * dic = self.dataArray[indexPath.row];
    cell.timeLable.text = dic[@"dateline"];
    cell.guahaoHospitalname.text = dic[@"hospital"];
    

    //显示挂号单图片
    [cell.guahaoImgView setImageWithURL:[NSURL URLWithString:dic[@"pic"]] placeholderImage:[UIImage imageNamed:@"公用_默认图片.png"]];
    cell.guahaoImgView.clipsToBounds = YES;
    cell.guahaoImgView.layer.cornerRadius = 3;
    
    UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapImgView:)];
    cell.guahaoImgView.tag=indexPath.row+100;
    cell.guahaoImgView.userInteractionEnabled = YES;
    [cell.guahaoImgView addGestureRecognizer:tap];

    return cell;
}
//图片的触发手势--点击使图片放大
-(void)tapImgView:(UITapGestureRecognizer*)tap
{
    NSDictionary * dic = self.dataArray[tap.view.tag-100];
    MyNSLog("%@", dic[@"pic"]);
    NSString *bigImageUrl = [[dic[@"pic"] componentsSeparatedByString:@"@"] objectAtIndex:0];
    UIImageView * tempImageview = (UIImageView *)[self.view viewWithTag:tap.view.tag];
    self.picViewController = [[PictureViewController alloc] initWithThumbImage:tempImageview.image imageView:tempImageview largeImagePath:bigImageUrl];
     self.picViewController.delegate = self;
    
}

-(void)PictureViewDidDismiss{
    [self.tableView reloadData];
    self.picViewController.imageView=nil;
    self.picViewController = nil;
}


@end
